var core = require('core');
var db = core.db;
var send = core.send;

var qrcode = function(req, res, next){
    var body = req.body;
    var SEND;
    var query = {
        openid : body.openid,
        tagid_list : core.config.wechat.tagid_contrast[1]
    };

    var options = {
        upsert : true
    };

    db.schema.User.findOne(query, function(err, doc){
        if(err){
            console.error(err.code);
            SEND = new send(100001);
        }else{
            if(!err&&!doc){
                SEND = new send(100200);
                res.send(SEND);
            }else{
                delete body.openid;
                var temptoken = new db.schema.TempToken({data: body});
                temptoken.save(function(err, docs){
                    SEND = new send(0);
                    SEND.docs = docs;
                    res.send(SEND);
                });
            }
        };
    });



};

module.exports = qrcode;
